package control
{
	import com.greensock.easing.Cubic;
	import com.greensock.TweenLite;
	import events.MyEvent;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	/**
	 * ...
	 * @author FireKnight
	 */
	public class ScrollObject extends EventDispatcher
	{
		public var container:Sprite;
		public var _up_bt:DisplayObject;
		public var _down_bt:DisplayObject;
		public var _mask_mc:DisplayObject;
		public var _hitArea:Sprite;
		public var distance:Number = 0;
		protected var startMouseY:Number = 0;
		protected var endMouseY:Number = 0;
		public var target:Sprite;
		public var fx:int = 0;
		public var dif:Number = 0;//上下偏差
		public function ScrollObject(target:IEventDispatcher = null)
		{
			super(target);
			trace("ScrollObject 2015.10.11");
		}
		
		/**
		 *
		 * @param	s		内容
		 * @param	_width
		 * @param	_x
		 * @param	_y
		 * @param	_font	字体
		 * @param	color	字体颜色
		 * @param	size 	字体大小
		 * @param	leading 行距
		 * @param	indent	从左边距到段落中第1个字符的缩进
		 */
		public function text(s:String, _width:Number, _x:Number, _y:Number, _font:Font, color:Number = 0x181818, size:Number = 18, leading:Number = 3, indent:Number = 30):void
		{
			var desTextField:TextField = new TextField();
			trace("ScrollObject 2015.10.11");
			desTextField.wordWrap = true;
			desTextField.multiline = true;
			desTextField.type = "dynamic";
			desTextField.autoSize = TextFieldAutoSize.LEFT;
			desTextField.embedFonts = true;
			desTextField.selectable = false;
			desTextField.htmlText = s;
			desTextField.width = _width;
			desTextField.border = true;
			var textFormat:TextFormat = new TextFormat();
			textFormat.font = _font.fontName;
			trace("_font.fontName="+_font.fontName);
			textFormat.leading = leading;
			textFormat.indent = indent;
			textFormat.size = size;
			textFormat.color = color;
			desTextField.defaultTextFormat = textFormat;
			desTextField.setTextFormat(textFormat);
//			container = new Sprite();
//			container.addChild(desTextField);
//			container.alpha = 0;
			desTextField.x = _x;
			desTextField.y = _y;
			target.addChild(desTextField);
//			container.mask = _mask_mc;
		
		}
		
		/**
		 *
		 * @param	s		container
		 * @param	b		target
		 * @param	mask
		 * @param	_x
		 * @param	_y
		 */
		public function addImg(s:Sprite, b:Sprite, mask:MovieClip, _x:Number, _y:Number):void
		{
			container = s;
			container.x = _x;
			container.y = _y;
			b.addChild(container);
			container.mask = mask;
		}
		
		public function addHitAreaEvent():void
		{
			_hitArea.addEventListener(MouseEvent.MOUSE_DOWN, handleMaskMouseDown);
		}
		
		public function removeHitAreaEvent():void
		{
			if (_hitArea)
			{
				if (_hitArea.hasEventListener(MouseEvent.MOUSE_DOWN))
				{
					_hitArea.removeEventListener(MouseEvent.MOUSE_DOWN, handleMaskMouseDown);
				}
			}
		}
		
		protected function handleMouseUp(e:MouseEvent):void
		{
			
			target.parent.stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			target.parent.stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
		}
		
		protected function handleMaskMouseDown(e:MouseEvent):void
		{
			distance = 0;
//			trace("target="+target);
//			trace("target.parent="+target.parent);
//			trace("_hitArea="+_hitArea);
//			trace("target.parent.mouseX ="+target.parent.mouseX );
//			trace("target.parent.mouseY ="+target.parent.mouseY );
//			trace("_hitArea.x ="+_hitArea.x );
//			trace("_hitArea.y ="+_hitArea.y );
			if (target.parent.mouseX > (_hitArea.x + target.x) && target.parent.mouseX < (_hitArea.x + _hitArea.width + target.x) && target.parent.mouseY > _hitArea.y + target.y && target.parent.mouseY < (_hitArea.y + _hitArea.height + target.y))
			{
				startMouseY = target.parent.mouseY;
				trace(container.y);
				target.parent.stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
				target.parent.stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
			}
		}
		
		
		protected function handleMouseMove(e:MouseEvent):void
		{
			endMouseY = target.parent.mouseY;
			distance = startMouseY - endMouseY;
			moveFunction();
		}
		
		protected function moveFunction():void
		{
			var targetY:Number = 0 - distance;
			var _target:Number = container.y + targetY;
			if (_target > _mask_mc.y+dif)
				_target = _mask_mc.y+dif;
			if (_target < _mask_mc.y - container.height + _mask_mc.height-dif)
				_target = _mask_mc.y - container.height + _mask_mc.height-dif;
			TweenLite.to(container, 0.5, { y: _target, ease: Cubic.easeOut,onUpdate:dispath,onUpdateParams:[container.y],onComplete: showBt } );
		}
		
		
		protected function dispath(s:Number):void
		{
			this.dispatchEvent(new MyEvent("scrolled", s));
		}
		
		public function removeBtEvent():void
		{
			if(_up_bt)
			{
				if (_up_bt.hasEventListener(MouseEvent.MOUSE_DOWN))
				{
					_up_bt.removeEventListener(MouseEvent.MOUSE_DOWN, handleBtMouseDown);
					_down_bt.removeEventListener(MouseEvent.MOUSE_DOWN, handleBtMouseDown);
				}
			}
		
		}
		
		public function addBtEvent():void
		{
			if(_up_bt)
			{
				_up_bt.addEventListener(MouseEvent.MOUSE_DOWN, handleBtMouseDown);
				_down_bt.addEventListener(MouseEvent.MOUSE_DOWN, handleBtMouseDown);
			}
			
		}
		
		protected function handleBtMouseDown(e:MouseEvent):void
		{
			e.stopPropagation();
			switch (e.currentTarget)
			{
				case _up_bt: 
					fx = 6;
					target.addEventListener(MouseEvent.MOUSE_UP, handleBtMouseUp);
					_up_bt.addEventListener(Event.ENTER_FRAME, handleBtEnterFrame);
					break;
				case _down_bt: 
					fx = -6;
					target.addEventListener(MouseEvent.MOUSE_UP, handleBtMouseUp);
					_up_bt.addEventListener(Event.ENTER_FRAME, handleBtEnterFrame);
					break;
			}
		}
		
		protected function handleBtMouseUp(e:MouseEvent):void
		{
			fx = 0;
			target.removeEventListener(MouseEvent.MOUSE_UP, handleBtMouseUp);
			_up_bt.removeEventListener(Event.ENTER_FRAME, handleBtEnterFrame);
		}
		
		protected function handleBtEnterFrame(e:Event):void
		{
			if (fx == 6)
			{
				if (container.y < _mask_mc.y+dif)
				{
					container.y += fx;
				}
			}
			else if (fx == -6)
			{
				if (container.y + container.height > (_mask_mc.y + _mask_mc.height-dif))
				{
					container.y += fx;
				}
			}
			showBt();
			this.dispatchEvent(new MyEvent("scrolled", container.y));
		}
		
		public function showBt():void
		{
			if(!_up_bt)return;
			if (container.height < _mask_mc.height)
			{
				_up_bt.visible = false;
				_down_bt.visible = false;
			}
			else
			{
				
				if (container.y + container.height > (_mask_mc.y + _mask_mc.height-dif))
				{
					_down_bt.visible = true;
				}
				else
				{
					_down_bt.visible = false;
				}
				
				if (container.y < _mask_mc.y+dif)
				{
					_up_bt.visible = true;
				}
				else
				{
					_up_bt.visible = false;
				}
			}
			this.dispatchEvent(new MyEvent("scrolled", container.y));
		}
		
		public function init():void
		{
			trace("container.height ="+container.height );
			trace("_mask_mc.height ="+_mask_mc.height );
			if (container.height > _mask_mc.height)
			{
				addBtEvent();
				addHitAreaEvent();
			}
			showBt();
		}
		
		public function gc():void
		{
			removeBtEvent();
			removeHitAreaEvent();
		}
	
	}

}